package com.sara.controller;

import com.sara.entity.Permission;
import com.sara.exception.ServiceException;
import com.sara.service.PermissionService;
import com.sara.shiro.MyFilterChainDefinition;
import com.sara.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/manage/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private MyFilterChainDefinition myFilterChainDefinition;


    @GetMapping
    public String permissionList(Model model){
        List<Permission> permissionList = permissionService.findAllPermission();
        model.addAttribute("permissionList",permissionList);
        System.out.println("从权限这里过了。。。。。。。。。。。。。");
        return "manage/permission/list";
    }


    @GetMapping("/add")
    public String permissionAddPage(Model model){
        List<Permission> permissionListAndTypeIsMenu = permissionService.findPermissionByType(Permission.PERMISSION_TYPE_MENU);
        model.addAttribute("permissionListAndTypeIsMenu", permissionListAndTypeIsMenu);
        return "manage/permission/add";
    }

    @PostMapping("/add")
    public String add(Permission permission, RedirectAttributes attributes){
        permissionService.save(permission);
        attributes.addFlashAttribute("message",Constant.SEND_PAGE_MASSAGE_ADD_SECCESS);
        myFilterChainDefinition.updatePermission();
        return "redirect:/manage/permission";
    }

    @GetMapping("/edit/{id:\\d+}")
    public String permissionEditPage(@PathVariable Integer id, Model model){
         Permission permission = permissionService.findPermissionById(id);
        model.addAttribute("permission",permission);

        List<Permission> permissionListAndTypeIsMenu = permissionService.findPermissionByType(Permission.PERMISSION_TYPE_MENU);
        permissionListAndTypeIsMenu.remove(permission);

        List<Permission> permissionList = permissionService.findPermissionWithPid(permission.getId());
        for (Permission temp : permissionList){
            permissionListAndTypeIsMenu.remove(temp);
        }


        //permissionList.remove(permission);
        model.addAttribute("permissionList",permissionListAndTypeIsMenu);
        return "manage/permission/edit";

    }

    @PostMapping("/edit")
    public String permissionEdit(Permission permission, RedirectAttributes attributes){
        permissionService.edit(permission);
        attributes.addFlashAttribute("message", "修改成功!");
        myFilterChainDefinition.updatePermission();
        return "redirect:/manage/permission";
    }


    @GetMapping("/delete/{id:\\d+}")
    public String permissionDelete(@PathVariable Integer id, RedirectAttributes attributes )throws  ServiceException{
        try {
            permissionService.delete(id);
            attributes.addFlashAttribute("message", "删除成功");
            myFilterChainDefinition.updatePermission();
        } catch (ServiceException e) {
            attributes.addFlashAttribute("message", e.getMessage());
        }
        return "redirect:/manage/permission";
    }


}
